package com.healthbox.resultsservice.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class SendMQMessage {
    static Message message;
    static MessageProperties messageProperties;
    static RabbitTemplate rabbitTemplate;

    public SendMQMessage(){
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setAddresses("106.3.97.187:5672");
        cachingConnectionFactory.setUsername("admin");
        cachingConnectionFactory.setPassword("admin");
        cachingConnectionFactory.setVirtualHost("/HealthBox");
        rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
    }

    public void send(String phone){
        messageProperties = MessagePropertiesBuilder.newInstance().setContentType("RESULT").setHeader("HealthBox","RESULT").build();
        message = MessageBuilder.withBody(phone.getBytes()).andProperties(messageProperties).build();
        rabbitTemplate.convertAndSend("amq.direct", "direct", message);
    }

    public void send(Object appointmentInfo){
        messageProperties = MessagePropertiesBuilder.newInstance().setContentType("APPOINTMENT").setHeader("HealthBox","APPOINTMENT").build();
        message = MessageBuilder.withBody(JSON.toJSONBytes(appointmentInfo)).andProperties(messageProperties).build();
        rabbitTemplate.convertAndSend("amq.direct", "direct", message);
    }
}
